/*****************************************************
 * The map
 *****************************************************/
var map;
$().ready(loadMap);
$().unload(GUnload);

function loadMap() {
	
	if (GBrowserIsCompatible()) {
		
		setMapSize();
		$(window).resize(setMapSize);
		
		map = new GMap2(document.getElementById('map')); 
		
		var parisCoordonates = new GLatLng(0, 0);
		map.setCenter(parisCoordonates, 2);
		map.enableScrollWheelZoom(); 
		map.addControl(new GMapTypeControl());
		
		//initializeToolbar();
		initializeGEvents();
	}
	else
	{
		// TODO : Redirect visitors with incompatible browsers
	}
}


function setMapSize() {
	var screenW = (window.innerWidth) ? window.innerWidth : document.documentElement.clientWidth;
	var screenH = (window.innerHeight) ? window.innerHeight : document.documentElement.clientHeight;
	$('#map').css({width:screenW+"px",height:screenH+"px"});
}

// Listeners on the Map
function initializeGEvents()
{	
	GEvent.addListener(map,'dragstart',draggingMap);
	GEvent.addListener(map,'dragend',stopDraggingMap);
}

// When map is dragged, elements go to lower opacity
function draggingMap()
{
		$('.window').css({'opacity':'0.15'});
		$('.window').unbind('mouseover').unbind('mouseout');
		$('#photobar').css({'opacity':'0.15'});
		$('#tagbar').css({'opacity':'0.15'});
		$('#photobar').unbind('mouseover',opacifyPhotobar).unbind('mouseout',desOpacifyPhotobar);
		$('#tagbar').unbind('mouseover',opacifyTagbar).unbind('mouseout',desOpacifyTagbar);
}

function stopDraggingMap()
{
		$('.window').css({'opacity':'0.9'});
		
		$('#blogEntry').bind('mouseover',
			function(){
				$(this).css({'zIndex':'100','opacity':'1'});
			}).bind('mouseout',
			function(){
				$(this).css({'zIndex':'10','opacity':'0.90'});
			});
		
		$('#comments').bind('mouseover',
			function(){
				$(this).css({'zIndex':'100','opacity':'1'});
			}).bind('mouseout',
			function(){
				$(this).css({'zIndex':'10','opacity':'0.90'});
			});
		
		$('#addComment').bind('mouseover',
			function(){
				$(this).css({'zIndex':'100','opacity':'1'});
			}).bind('mouseout',
			function(){
				$(this).css({'zIndex':'10','opacity':'0.90'});
			});
		
		$('#photobar').css({'opacity':'0.7'});
		$('#photobar').bind('mouseover',opacifyPhotobar).bind('mouseout',desOpacifyPhotobar);
		
		$('#tagbar').css({'opacity':'1'});
		$('#tagbar').bind('mouseover',opacifyTagbar).bind('tagbar',desOpacifyTagbar);
		
}

function opacifyPhotobar()
{
		$('#photobar').css('opacity',1)
}

function desOpacifyPhotobar()
{
		$('#photobar').css('opacity',0.7)
}

function opacifyTagbar()
{
		$('#tagbar').css('opacity',1)
}

function desOpacifyTagbar()
{
		$('#tagbar').css('opacity',0.7)
}

/*****************************************************
 * Markers
 * Code bellow adapted from previous open source work
 * @see http://code.google.com/p/gmaps-samples/
 *****************************************************/

/* 
function initializeToolbar()
{
	$('#hand_b').click(stopEditing);
	$('#placemark_b').click(placeMarker);
	$('#line_b').click(startLine);
	
	select("#hand_b");
}
*/

function select(id) 
{
	$('#hand_b').removeClass('selected');
	$('#placemark_b').removeClass('selected');
	$('#line_b').removeClass('selected');
	$(id).addClass('selected');
}

function stopEditing()
{
	select('#hand_b');
	map.enableDragging();
	$('#map').css('cursor','default');
}

function placeMarker()
{
	select('#placemark_b');
	
	map.disableDragging();
	$('#map').css('cursor',"url('img/cross.cur'), auto");
	
	var listener = GEvent.addListener(map, 'click', function(overlay, latlng) {
    if (latlng) {
			
      GEvent.removeListener(listener);
			stopEditing();
			
      var marker = new GMarker(latlng, {icon: getIcon(), draggable: true});
      map.addOverlay(marker);
      GEvent.addListener(marker, "click", function() {
        // @todo : infoBox pour changer l'icone. cf http://www.visual-case.it/cgi-bin/vc/GMapsIcons.pl
      });	
    }
  });
}

function getIcon() {
  var icon = new GIcon();
  icon.image = 'img/blue-dot.png';
  icon.iconSize = new GSize(32, 32);
  icon.iconAnchor = new GPoint(15, 32);
	
  return icon;
}

function startLine()
{
	select('#line_b');
	alert('do we have to implement it ?');
}